package com.openarena.statistics;

import com.openarena.model.Game;
import com.openarena.model.Player;
import java.io.Serializable;
import java.util.List;

public class PlayerSummaryStat implements Serializable, Comparable<PlayerSummaryStat> {

    private final Player player;
    private Integer totalPoints;
    private Integer matchesPlayed;
    private Integer matchesWon;
    private Integer playedSeconds;
    private Integer kills;
    private Integer deaths;
    private Integer timesMVP;

    public PlayerSummaryStat(Player player) {
        if (player == null) {
            throw new NullPointerException("Player can not be null");
        }
        this.player = player;

        totalPoints = 0;
        matchesPlayed = 0;
        matchesWon = 0;
        timesMVP = 0;
        playedSeconds = 0;
        kills = 0;
        deaths = 0;
    }

    public Integer getDeaths() {
        return deaths;
    }

    public Double getEffectiveness() {
        return (matchesPlayed > 0) ? (double) matchesWon / matchesPlayed : 0;
    }

    public String getFormattedEffectiveness() {
        return String.format("%.4g%n", 100 * getEffectiveness());
    }

    public Double getKdRatio() {
        return (deaths > 0) ? (double) kills / deaths : 0;
    }

    public String getFormattedKdRatio() {
        return String.format("%.3g%n",getKdRatio());
    }

    public Integer getKills() {
        return kills;
    }

    public Integer getMatchesPlayed() {
        return matchesPlayed;
    }

    public Integer getPlayedMinutes() {
        return playedSeconds / 60;
    }

    public Player getPlayer() {
        return player;
    }

    public Double getPointsPerMinute() {
        return (playedSeconds > 0) ? ((double) totalPoints / playedSeconds) * 60 : 0;
    }

    public String getFormattedPointsPerMinute() {
        return String.format("%.3g%n", getPointsPerMinute());
    }

    public Integer getTimesMVP() {
        return timesMVP;
    }

    public Double getTimesMVPPerMatch() {
        return (matchesPlayed > 0) ? (double) timesMVP / matchesPlayed : 0;
    }

    public String getFormattedTimesMVPPerMatch() {
        return String.format("%.3g%n", getTimesMVPPerMatch());
    }

    public Integer getMatchesWon() {
        return matchesWon;
    }

    void addMatchWon() {
        matchesWon++;
    }

    void addTimeMVP() {
        timesMVP++;
    }

    void addMatchPlayed() {
        matchesPlayed++;
    }

    void addPlayedSeconds(Integer duration) {
        playedSeconds += duration;
    }

    void addPoints(Integer points) {
        totalPoints += points;
    }

    void addKills(Integer kills) {
        this.kills += kills;
    }

    void addDeaths(Integer deaths) {
        this.deaths += deaths;
    }

    @Override
    public int compareTo(PlayerSummaryStat t) {
        return this.player.compareTo(t.player);
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final PlayerSummaryStat other = (PlayerSummaryStat) obj;
        if (this.player != other.player && (this.player == null || !this.player.equals(other.player))) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        return player.hashCode();
    }

}
